﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_Staff : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_Staff()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;

            ComboboxItem item = new ComboboxItem();
            item.Text = "Male";
            item.Value = "1";
            this.sexValue.Properties.Items.Add(item);

            ComboboxItem item2 = new ComboboxItem();
            item2.Text = "Female";
            item2.Value = "2";
            this.sexValue.Properties.Items.Add(item2);

            LoadDepartment();            
        }

        private void LoadDepartment()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT [DepartmentID] FROM [222_Staff]"
                    + " WHERE [DepartmentID] IS NOT NULL GROUP BY [DepartmentID] ORDER BY [DepartmentID]";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["DepartmentID"].ToString().Trim();
                    item.Text = dataReader["DepartmentID"].ToString().Trim();
                    this.departmentID.Properties.Items.Add(item);
                }
                sql_cmd.Dispose();
            }
            catch
            {
                MessageBox.Show("Cannot load Department List");
            }
            finally
            {
                myConnection.Close();
            }
        }

        private void LoadGroupID()
        {
            this.groupID.Properties.Items.Clear();
            if (this.departmentID.SelectedIndex >= 0)
            {
                ComboboxItem department = (ComboboxItem)this.departmentID.SelectedItem;
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT [GroupID] FROM [222_Staff]"
                        + " WHERE [DepartmentID] = '" + department.Value.ToString() + "'"
                        + " AND [GroupID] IS NOT NULL"
                        + " GROUP BY [GroupID] ORDER BY [GroupID]";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    while (dataReader.Read())
                    {
                        ComboboxItem item = new ComboboxItem();
                        item.Value = dataReader["GroupID"].ToString().Trim();                        
                        switch (dataReader["GroupID"].ToString().Trim())
                        {
                            case "C":
                                item.Text = "Controller";
                                break;
                            case "RV":
                                item.Text = "RV";
                                break;
                            case "RB":
                                item.Text = "Robot";
                                break;
                            case "SYS":
                                item.Text = "System";
                                break;
                            case "W":
                                item.Text = "Wire";
                                break;
                            default:
                                item.Text = dataReader["GroupID"].ToString().Trim();
                                break;
                        }
                        this.groupID.Properties.Items.Add(item);
                    }
                    sql_cmd.Dispose();
                }
                catch
                {
                    MessageBox.Show("Cannot load Group ID");
                }
                finally
                {
                    myConnection.Close();
                }
            }
        }

        private void LoadSubGroup()
        {
            this.subGroup.Properties.Items.Clear();
            if (this.groupID.SelectedIndex >= 0)
            {
                ComboboxItem group = (ComboboxItem)this.groupID.SelectedItem;
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT [Sub-Group] FROM [222_Staff]"
                        + " WHERE [GroupID] = '" + group.Value.ToString() + "'"
                        + " AND [Sub-Group] IS NOT NULL"
                        + " GROUP BY [Sub-Group] ORDER BY [Sub-Group]";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    while (dataReader.Read())
                    {
                        ComboboxItem item = new ComboboxItem();
                        item.Value = dataReader["Sub-Group"].ToString().Trim();
                        switch (dataReader["Sub-Group"].ToString().Trim())
                        {
                            case "MOU":
                                item.Text = "Mouter";
                                break;
                            case "ASS":
                                item.Text = "Assembly";
                                break;
                            case "FIN":
                                item.Text = "Finish";
                                break;
                            default:
                                item.Text = dataReader["GroupID"].ToString().Trim();
                                break;
                        }
                        this.subGroup.Properties.Items.Add(item);
                    }
                    sql_cmd.Dispose();
                }
                catch
                {
                    MessageBox.Show("Cannot load Group ID");
                }
                finally
                {
                    myConnection.Close();
                }
            }
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string staff_id = null;
            string staff_name = null;
            string sec_id = null;
            string sec_name = null;
            DateTime birthday = new DateTime();
            DateTime hiredate = new DateTime();
            DateTime stopdate = new DateTime();
            int sex = 0;
            string groupid = null;
            string subgroup = null;
            string personalinf = null;
            string department = null;
            string education = null;
            string address = null;
            string phonenumber = null;

            if (string.IsNullOrEmpty(this.staffID.Text))
            {
                error = "Please enter Staff ID";
            }
            else
            {
                staff_id = this.staffID.Text;
            }
            if (string.IsNullOrEmpty(this.staffName.Text))
            {
                error = "Please enter Staff Name";
            }
            else
            {
                staff_name = this.staffName.Text;
            }
            if (string.IsNullOrEmpty(error))
            {
                sec_id = this.secID.Text;
                sec_name = this.secName.Text;
                groupid = this.groupID.Text;
                subgroup = this.subGroup.Text;
                personalinf = this.personalInf.Text;
                if (this.sexValue.SelectedIndex == 0)
                {
                    sex = 1;
                }
                else
                {
                    sex = 0;
                }
                department = this.departmentID.Text;
                education = this.educationValue.Text;
                address = this.addressValue.Text;
                phonenumber = this.phoneNumber.Text;
                            
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_Staff] ([StaffID], [StaffName], [Birthday], [HireDate],"
                        + " [StopDate], [SecID], [SecName], [GroupID], [Sub-Group], [PersonalInf], [Sex], [DepartmentID],"
                        + " [Education], [PhoneNumber], [Address])"
                        + " VALUES ('" + staff_id + "', N'" + staff_name + "', @birthday, @hiredate, @stopdate, '" + sec_id + "',"
                        + " N'" + sec_name + "', '" + groupid + "', '" + subgroup + "', N'" + personalinf + "', " + sex + ","
                        + " '" + department + "', N'" + education + "', '" + phonenumber + "', N'" + address + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    if (this.birthDay.EditValue != null)
                    {
                        birthday = DateTime.Parse(this.birthDay.EditValue.ToString());
                        cmd_sql.Parameters.Add("@birthday", SqlDbType.DateTime).Value = birthday.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@birthday", SqlDbType.DateTime).Value = DBNull.Value;
                    }
                    if (this.hireDate.EditValue != null)
                    {
                        hiredate = DateTime.Parse(this.hireDate.EditValue.ToString());
                        cmd_sql.Parameters.Add("@hiredate", SqlDbType.DateTime).Value = hiredate.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@hiredate", SqlDbType.DateTime).Value = DBNull.Value;
                    }                    
                    if (this.stopDate.EditValue != null)
                    {
                        stopdate = DateTime.Parse(this.stopDate.EditValue.ToString());
                        cmd_sql.Parameters.Add("@stopdate", SqlDbType.DateTime).Value = stopdate.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@stopdate", SqlDbType.DateTime).Value = DBNull.Value;
                    }                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Staff added success.");
                    clearForm();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Staff. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }

        private void departmentID_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGroupID();
        }

        private void groupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadSubGroup();
        }
    }
}
